package com.clearliang.wxredpacket.view;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.clearliang.wxredpacket.redPacketService.OpenAccessibilitySettingHelper;
import com.clearliang.wxredpacket.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btnStart;
    private Button btnCheck;
    private Button btnClear;
    private TextView tvNum;
    private TextView tvAmount;

    private MyReceiver receiver;
    private IntentFilter intentFilter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();

        receiver = new MyReceiver();
        intentFilter = new IntentFilter();
        intentFilter.addAction("android.accessibility.Connection");
        registerReceiver(receiver, intentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    private void initEvent() {
        btnStart.setOnClickListener(this);
        btnCheck.setOnClickListener(this);
        btnClear.setOnClickListener(this);
    }

    private void initView() {
        btnStart = (Button) findViewById(R.id.btn_start);
        btnCheck = (Button) findViewById(R.id.btn_check);
        tvNum = (TextView) findViewById(R.id.tv_num);
        tvAmount = (TextView) findViewById(R.id.tv_amount);
        btnClear = (Button) findViewById(R.id.btn_clear);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_start:
                OpenAccessibilitySettingHelper.jumpToSettingPage(getApplicationContext());// 跳转到开启页面
                break;

            case R.id.btn_check:
                if (OpenAccessibilitySettingHelper.isAccessibilitySettingsOn(getApplicationContext())) {
                    Toast toast = Toast.makeText(MainActivity.this, "服务已开启", Toast.LENGTH_LONG);
                    //toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                } else {
                    Toast toast = Toast.makeText(MainActivity.this, "服务未开启", Toast.LENGTH_LONG);
                    //toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                }
                break;

            case R.id.btn_clear:
                SPUtils.getInstance("Info").put("num",0);
                SPUtils.getInstance("Info").put("amount","0.00");

                tvNum.setText("0 个");
                tvAmount.setText("0.00 元");
                ToastUtils.showShort("记录已清除");
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        int num = SPUtils.getInstance("Info").getInt("num",0);
        String amount = SPUtils.getInstance("Info").getString("amount","0.00");
        tvNum.setText(num + " 个");
        tvAmount.setText(amount + " 元");
        LogUtils.e(num,amount);

        if (OpenAccessibilitySettingHelper.isAccessibilitySettingsOn(getApplicationContext())) {
            btnStart.setText("关闭服务");
        }else {
            btnStart.setText("开启服务");
        }
    }

    //自定义广播接收器
    class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            String msg = bundle.getString("msg");
            boolean isOn = bundle.getBoolean("isOn");
            if (isOn) {
                btnStart.setText("关闭服务");
            } else {
                btnStart.setText("开启服务");
            }
        }
    }

    @Override
    public void onBackPressed() {
        new android.app.AlertDialog.Builder(this)
                .setTitle("提示")
                .setMessage("是否需要退出程序？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ActivityUtils.finishAllActivities();
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }
}
